<template>
  <div>
    <div>
      <div class="header">
        <el-input v-model="question" placeholder="请输入问题"></el-input>
        <el-button type="primary" @click="search">
          <svg-icon icon-class="search" />
          查找
        </el-button>
        <el-button type="primary" @click="add">
          <i class="el-icon-edit" />
          添加
        </el-button>
        <el-button type="primary" @click="excelData">
          <svg-icon icon-class="form" />
          导出
        </el-button>
      </div>

      <div class="table">
        <el-table
          border
          :data="tableData"
          style="width: 100%"
          :default-sort="{ prop: 'id', order: 'descending' }"
        >
          <el-table-column prop="id" width="120" label="问题ID" sortable>
          </el-table-column>
          <el-table-column prop="question" width="200" label="问题内容">
          </el-table-column>

          <el-table-column prop="answer" label="问题回复"> </el-table-column>
          <el-table-column width="200" label="操作">
            <template slot-scope="scope">
              <el-button
                @click="editclick(scope.row)"
                type="primary"
                size="small"
              >
                编辑
              </el-button>
              <el-button
                @click="delclick(scope.row)"
                type="danger"
                size="small"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div style="padding: 40px 0" class="footer">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>

      <div class="popup">
        <el-dialog
          :title="isEdit ? '编辑问题' : '添加问题'"
          :visible.sync="dialogFormVisible"
          :before-close="close"
        >
          <el-form :model="form" :rules="rules">
            <el-form-item label="问题" :label-width="formLabelWidth" prop="question">
              <el-input v-model="form.question" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="回复" :label-width="formLabelWidth" prop="answer">
              <el-input type="textarea" v-model="form.answer"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="close">取 消</el-button>
            <el-button type="primary" @click="subimt">确 定</el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import {getissue,addissue, updateissue, deleteissue } from "@/api/mall";
import { getToken } from "@/utils/auth";
import { formatDate } from "@/utils/time";
import { MessageBox, Message } from "element-ui";
export default {
  props: [],
  data() {
    return {
      tableData: [],
      page: 1,
      total: 10,
      limit: 10,
      question: "", //搜索问题
      dialogFormVisible: false, //是否显示弹出框
      formLabelWidth: "120px",
      form: {
        question: "", //问题内容
        answer: "", //问题回复
        id:"",//点击编辑传参id
      },
      isEdit: false,
      rules: {
          question: [
            { required: true, message: '请输入问题名称', trigger: 'blur' },
          ],
          answer: [
            { required: true, message: '请输入问题回复', trigger: 'blur' },
          ],
      }
    };
  },
  mounted() {
    this.getdata();
  },
  methods: {
    getdata() {
      let params = {
        page: this.page,
        limit: this.limit,
        question:this.question
      };
      getissue(params).then((res) => {
        console.log(res);
        if (res.errno == 0) {
          this.tableData = res.data.items;
          this.total = res.data.total;
        }
      });
    },
    // 改变每一页多少条数据
    handleSizeChange(e) {
      this.limit = e;
      this.getdata();
    },
    //改变当前页数
    handleCurrentChange(e) {
      this.page = e;
      this.getdata();
    },
    //搜索
    search() {
      this.getdata();
    },
    //点击添加
    add() {
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    close(){
        this.isEdit = false;
        this.dialogFormVisible=false
    },
    //弹出框确定
    subimt(e) {
      if (this.isEdit == true) {
        this.isEdit = false;
        if (
          this.form.question == "" &&
          this.form.answer == "" 
        ) {
          Message({
            message: "数据不完整",
            type: "error",
            duration: 2 * 1000,
          });
          return;
        }
        let data = {
          question:this.form.question,
          answer:this.form.answer,
          id:this.form.id
        };
        updateissue(data).then((res) => {
          console.log(res);
          if (res.errno == 0) {
            this.dialogFormVisible=false
            for(let k in this.form){
              this.form[k]=""
            }
            
            this.getdata();
          }
        });
      } else {
        if (
          this.form.question == "" &&
          this.form.answer == "" 
        ) {
          Message({
            message: "数据不完整",
            type: "error",
            duration: 2 * 1000,
          });
          return;
        }
        let data = {
          question:this.form.question,
          answer:this.form.answer
        };

        addissue(data).then((res) => {
          console.log(res);
          
          if (res.errno == 0) {
            this.dialogFormVisible = false;
            for(let k in this.form){
              this.form[k]=""
            }
            
            this.getdata();
          }
        });
      }
    },
    //点击编辑
    editclick(e) {
      console.log(e);
      this.isEdit = true;
      this.form.question=e.question
      this.form.answer=e.answer
      this.form.id = e.id;

      this.dialogFormVisible = true;
    },
    //点击删除
    delclick(e) {
      let data = {
        id:e.id
      };
      deleteissue(data).then((res) => {
        if (res.errno == 0) {
          Message({
            message: "删除成功",
            type: "success",
            duration: 2 * 1000,
          });
          this.getdata();
        }
      });
    },
    //导出excel表格
    excelData() {
      const headers = {
        问题ID: "id",
        问题内容: "question",
        问题回复: "answer",
      };
      //懒加载
      import("@/vendor/Export2Excel").then((excel) => {
        const data = this.formatJson(headers, this.tableData);
        excel.export_json_to_excel({
          header: Object.keys(headers),
          data,
          filename: "通用问题",
        });
      });
    },
    // 该方法负责将数组转化成二维数组
    formatJson(headers, rows) {
      //首先遍历数组
      return rows.map((item) => {
        // item是对象  => 转化成只有值的数组 => 数组值的顺序依赖headers  {username: '张三'  }
        // Object.keys(headers)  => ["姓名", "手机号",...]
        return Object.keys(headers).map((key) => {
          return item[headers[key]]; // 得到 ['张三'，’129‘，’dd‘,'dd']
        });
      });
    },
  },
  computed: {},
};
</script>
<style lang="less" scoped>
.header {
  padding: 15px;
  display: flex;
  flex-wrap: wrap;
  /deep/.el-input {
    width: 200px;
    margin-left: 15px;
    .el-input__inner {
      width: 200px;
    }
  }
  .el-button {
    margin-left: 15px;
  }
}
.table {
  padding: 15px;
  /deep/.cell {
    text-align: center;
    img {
      width: 40px;
      height: 40px;
    }
  }
}
</style>
